package com.vmj.classfile.entity;

import com.vmj.classfile.entity.attribute.Attribute;
import com.vmj.classfile.entity.attribute.SourceFileAttribute;

import java.util.List;

/**
 * Created by yangshoujun on 2017/8/5 下午11:26.
 */
public class ClassFile {

    String thisClassName;
    String magic;
    int minorVersion;
    int majorVersion;
    int constantPoolCount;
    ConstantPool constantPool;
    int accessFlag;
    int thisClass;
    int superClass;
    String superClassName;
    int interfaceCount;
    List<Integer> intefaces;
    List<String> interfaceNames;
    List<MemberInfo> fields;
    List<MemberInfo> methods;
    List<Attribute> attributes;

    public SourceFileAttribute getSourceFileAttribute() {
        for(Attribute attr: attributes) {
            if(attr instanceof SourceFileAttribute) {
                return (SourceFileAttribute) attr;
            }
        }

        return null;
    }

    public String getSourceFileName() {
        SourceFileAttribute sourceFileAttribute = getSourceFileAttribute();
        if(sourceFileAttribute == null) {
            return "Unknow";
        }

        return sourceFileAttribute.getSourceFileName() == null ? "Unknow" : sourceFileAttribute.getSourceFileName();
    }

    public String getMagic() {
        return magic;
    }

    public int getMinorVersion() {
        return minorVersion;
    }

    public int getMajorVersion() {
        return majorVersion;
    }

    public int getAccessFlag() {
        return accessFlag;
    }

    public int getThisClass() {
        return thisClass;
    }

    public int getSuperClass() {
        return superClass;
    }

    public List<MemberInfo> getFields() {
        return fields;
    }

    public List<MemberInfo> getMethods() {
        return methods;
    }

    public List<Attribute> getAttributes() {
        return attributes;
    }

    public void setMagic(String magic) {
        this.magic = magic;
    }

    public void setMinorVersion(int minorVersion) {
        this.minorVersion = minorVersion;
    }

    public void setMajorVersion(int majorVersion) {
        this.majorVersion = majorVersion;
    }

    public void setAccessFlag(int accessFlag) {
        this.accessFlag = accessFlag;
    }

    public void setThisClass(int thisClass) {
        this.thisClass = thisClass;
    }

    public void setSuperClass(int superClass) {
        this.superClass = superClass;
    }

    public void setFields(List<MemberInfo> fields) {
        this.fields = fields;
    }

    public void setMethods(List<MemberInfo> methods) {
        this.methods = methods;
    }

    public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }

    public int getConstantPoolCount() {
        return constantPoolCount;
    }

    public void setConstantPoolCount(int constantPoolCount) {
        this.constantPoolCount = constantPoolCount;
    }

    public ConstantPool getConstantPool() {
        return constantPool;
    }

    public void setConstantPool(ConstantPool constantPool) {
        this.constantPool = constantPool;
    }

    public int getInterfaceCount() {
        return interfaceCount;
    }

    public void setInterfaceCount(int interfaceCount) {
        this.interfaceCount = interfaceCount;
    }

    public List<Integer> getIntefaces() {
        return intefaces;
    }

    public void setIntefaces(List<Integer> intefaces) {
        this.intefaces = intefaces;
    }

    public String getThisClassName() {
        return thisClassName;
    }

    public String getSuperClassName() {
        return superClassName;
    }

    public List<String> getInterfaceNames() {
        return interfaceNames;
    }

    public void setThisClassName(String thisClassName) {
        this.thisClassName = thisClassName;
    }

    public void setSuperClassName(String superClassName) {
        this.superClassName = superClassName;
    }

    public void setInterfaceNames(List<String> interfaceNames) {
        this.interfaceNames = interfaceNames;
    }

    @Override
    public String toString() {
        return "ClassFile{" +
                "thisClassName='" + thisClassName + '\'' +
                ", superClassName='" + superClassName + '\'' +
                ", interfaceNames=" + interfaceNames +
                '}';
    }
}
